package com.ldfile.server.config;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author callmecool.ldsoft
 * @description tools
 * @since 2020/8/3 15:38
 */
@Configuration
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FileServerConfiguration implements WebMvcConfigurer {
    private final FileServiceInfoConfiguration fileServiceInfoConfiguration;

    /**
     * 访问图片方法
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 本地文件夹要以"file:" 开头，文件夹要以"/" 结束
        registry.addResourceHandler("/" + fileServiceInfoConfiguration.getPathYml() + "/**")
                .addResourceLocations("file:" + fileServiceInfoConfiguration.getDirYml() + "/");
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
                .maxAge(3600 * 24);
    }

}
